//Create Mortgage function
function Mortgage(vWidth) {
    var self = Titanium.UI.createView({
        backgroundColor: 'transparent',
    });
    var row1 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row2 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row3 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row4 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblMortgageAmount = Titanium.UI.createLabel({
        text: 'Mortgage Amount',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth - 2 * 10,
        textAlign: 'center'
    });
    var lblStampDuty = Titanium.UI.createLabel({
        text: 'Stamp Duty Payable',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth - 2 * 10,
        textAlign: 'center'
    });
    var txtMortgageAmount = Ti.UI.createTextField({
        editable: true,
        left: 10,
        textAlign: "center",
        hintText: '',
        right: 10,
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth - 2 * 10,
        color: 'black'
    });
    txtMortgageAmount.addEventListener('change', function (e) {
        Calc();
    });
    txtMortgageAmount.addEventListener('return', function (e) {
        txtMortgageAmount.blur();
    });
    var txtStampDuty = Ti.UI.createTextField({
        editable: false,
        left: 10,
        textAlign: "center",
        hintText: '',
        right: 10,
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth - 2 * 10,
        color: 'red'
    });
    var data = [row1, row2, row3, row4];
    row1.add(lblMortgageAmount);
    row2.add(txtMortgageAmount);
    row3.add(lblStampDuty);
    row4.add(txtStampDuty);
    
    var table = Ti.UI.createTableView({
        data: data,
        top: 0,
        backgroundColor: 'transparent',
        separatorColor: 'transparent'
    });
    self.add(table);
    function Calc () {
      var mortgageValue = parseInt(txtMortgageAmount.value);
      if (isNaN(mortgageValue) || mortgageValue === 0) {
      	txtStampDuty.value = '';
      } else{
      	var stampDuty = mortgageValue / 1000 * 4;
      	if (stampDuty > 500) {
      		stampDuty = 500;
      	};
      	txtStampDuty.value = stampDuty.toString();
      };
    }
    return self;
}
module.exports = Mortgage;